/* -*- Mode:Prolog; coding:ISO-8859-1; -*- */

:- dynamic stat/2.

set_stat(Key, Val) :- 
       retractall(stat(Key, _Val)), 
       assert(stat(Key, Val)).
        
get_stat(Key, Val) :- 
       stat(Key, Val).

increment_stat(Key) :-
        get_stat(Key, Val), !,
        VVal is Val + 1,
        set_stat(Key, VVal).


increment_stat(Key) :-
        set_stat(Key, 0).

increment_stat(Key, A) :-
        get_stat(Key, Val), !,
        VVal is Val + A,
        set_stat(Key, VVal).

increment_stat(Key, A) :-
        set_stat(Key, A).

initialise_statistics :-
        set_stat(explored_states, 0),
        set_stat(failed_branches, 0),
        set_stat(variables_found, 0),
        set_stat(average_n_states, 1),
        set_stat(iterations, 1),
        statistics(runtime,_).
        
show_statistics :- 
        get_stat(explored_states, ES),
        get_stat(failed_branches, FB),
        get_stat(variables_found, VF),
        get_stat(average_n_states, AS),
        get_stat(iterations, IT),
        statistics(runtime, [_, RT]),
        (ES < 1 -> ESS = 1 ; ESS = ES), 
        TPS is RT/ESS,
        print('#####STATS#####'), nl,
        print('Explored states: '), print(ES), nl,
        print('Failed branches: '), print(FB), nl,
        print('Variables found: '), print(VF), nl,
        print('Average number of open states per iteration: '), print(AS), nl,
        print('Iterations: '), print(IT), nl,
        print('Runtime: '), print(RT), nl,
        print('Time per state: '), print(TPS), nl.